package com.lanou.moving.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.adapter.CollectionAdapter;
import com.lanou.moving.adapter.LocalMusicAdapter;
import com.lanou.moving.adapter.RecentlyPlayAdapter;
import com.lanou.moving.base.BaseActivity;
import com.lanou.moving.base.BaseApplication;
import com.lanou.moving.entity.LocalMusicInfo;
import com.lanou.moving.entity.MusicInfo;
import com.lanou.moving.entity.dao.Collection;
import com.lanou.moving.entity.dao.CollectionDao;
import com.lanou.moving.entity.dao.RecentlyPlay;
import com.lanou.moving.entity.dao.RecentlyPlayDao;
import com.lanou.moving.eventBus.MyEventBus;
import com.lanou.moving.singleton.DaoSingleton;
import com.lanou.moving.utils.MyConfig;
import com.lanou.moving.utils.ScanLocalMusic;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by 刘琪 on 15/10/30.
 */
public class LocalMusicActivity extends BaseActivity implements
        AdapterView.OnItemClickListener, View.OnClickListener, AdapterView.OnItemLongClickListener {
    private ListView local_music_list;//本地音乐列表
    private ScanLocalMusic scanner;//扫描
    private List<LocalMusicInfo> localMusicInfos;//本地音乐
    private LocalMusicAdapter localmusicAdapter;
    private List<Collection> collections;//我的最爱
    private CollectionAdapter collectionAdapter;
    private List<RecentlyPlay> recentlyPlayList;//最近播放
    private RecentlyPlayAdapter recentlyPlayAdapter;
    // 标题栏
    private ImageView singer_back, search;
    private TextView singer;
    // 判断音乐来源
    private String resource;
    private CollectionDao collectionDao;
    private Collection delete_collection;
    private RecentlyPlayDao recentlyPlayDao;
    private RecentlyPlay delete_recentlyPlay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_music);
        BaseApplication.getInstance().addActivity(this);
        Intent intent = getIntent();
        resource = intent.getStringExtra("resource");
        init();
        recentlyPlayDao = DaoSingleton.getInstance().getRecentlyPlayDao();
        toIntent();
    }

    private void toIntent() {
        if (resource.equals("本地音乐")) {
            // 获取本地音乐
            scanner = new ScanLocalMusic();
            localMusicInfos = scanner.getLocalMusicInfos(LocalMusicActivity.this.getContentResolver());
            localmusicAdapter = new LocalMusicAdapter(getApplicationContext(), localMusicInfos);
            MyConfig.putSharePre(this, "localMusic", "count", localMusicInfos.size());
            scanner.setListAdpter(getApplicationContext(), localMusicInfos, local_music_list);
        } else if (resource.equals("我的最爱")) {
            collectionDao = DaoSingleton.getInstance().getCollectionDao();
            collections = collectionDao.loadAll();
            MyConfig.putSharePre(this, "collection", "count", collections.size());
            collectionAdapter = new CollectionAdapter(this, collections);
            local_music_list.setAdapter(collectionAdapter);
        } else if (resource.equals("最近播放")) {
            recentlyPlayDao = DaoSingleton.getInstance().getRecentlyPlayDao();
            recentlyPlayList = recentlyPlayDao.loadAll();
            MyConfig.putSharePre(this, "recentlyPlay", "count", recentlyPlayList.size());
            recentlyPlayAdapter = new RecentlyPlayAdapter(recentlyPlayList, this);
            local_music_list.setAdapter(recentlyPlayAdapter);
        }
    }

    private void init() {
        singer_back = (ImageView) findViewById(R.id.singer_back);
        singer_back.setOnClickListener(this);

        search = (ImageView) findViewById(R.id.search);
        search.setVisibility(View.GONE);

        singer = (TextView) findViewById(R.id.singer);
        singer.setText(resource);

        local_music_list = (ListView) findViewById(R.id.local_music_list);
        local_music_list.setOnItemClickListener(this);
        local_music_list.setOnItemLongClickListener(this);

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(LocalMusicActivity.this, PlayMusicActivity.class);
        RecentlyPlay entity = new RecentlyPlay();
        if (resource.equals("本地音乐")) {
            LocalMusicInfo localMusicInfo = (LocalMusicInfo) parent.getItemAtPosition(position);
            entity.setSongName(localMusicInfo.getTitle());
            entity.setSingerName(localMusicInfo.getArtist());
            entity.setDuration(localMusicInfo.getDuration());
            entity.setPlayUrl(localMusicInfo.getUrl());
            entity.setSongId(localMusicInfo.getId());
            entity.setResource("本地");
            recentlyPlayDao.insertOrReplace(entity);
            // 实体类之间的转换
            List<MusicInfo> musicInfoList = new ArrayList<>();
            for (LocalMusicInfo info : localMusicInfos) {
                MusicInfo musicInfo = new MusicInfo();
                musicInfo.setUrl(info.getUrl());
                musicInfo.setDuration(info.getDuration());
                musicInfo.setAlbum(info.getAlbum());
                musicInfo.setSingerName(info.getArtist());
                musicInfo.setSongName(info.getTitle());
                musicInfo.setResouce("本地");
                musicInfoList.add(musicInfo);
            }
            EventBus.getDefault().post(new MyEventBus.Deliver(musicInfoList, localMusicInfo.getTitle()));
            intent.putExtra("songName", localMusicInfo.getTitle());
            intent.putExtra("singerName", localMusicInfo.getArtist());
            intent.putExtra("duration", localMusicInfo.getDuration());
            intent.putExtra("resource", "本地");
        } else if (resource.equals("我的最爱")) {
            Collection collec = (Collection) parent.getItemAtPosition(position);
            entity.setSongName(collec.getSongName());
            entity.setSingerName(collec.getSingerName());
            entity.setDuration(collec.getDuration());
            entity.setPlayUrl(collec.getPlayUrl());
            entity.setResource("网络");
            entity.setSongId((long)collec.getSongId());
            recentlyPlayDao.insertOrReplace(entity);
            List<MusicInfo> musicInfoList = new ArrayList<>();
            for (Collection collection : collections) {
                MusicInfo musicInfo = new MusicInfo();
                musicInfo.setUrl(collection.getPlayUrl());
                musicInfo.setDuration(collection.getDuration());
                musicInfo.setSingerName(collection.getSingerName());
                musicInfo.setSongName(collection.getSongName());
                musicInfo.setResouce("本地");
                musicInfoList.add(musicInfo);
            }
            EventBus.getDefault().post(new MyEventBus.Deliver(musicInfoList, collec.getSongName()));
            intent.putExtra("songName", collec.getSongName());
            intent.putExtra("singerName", collec.getSingerName());
            intent.putExtra("duration", collec.getDuration());
            intent.putExtra("resource", "本地");
        } else if (resource.equals("最近播放")) {
            RecentlyPlay recentlyPlay = (RecentlyPlay) parent.getItemAtPosition(position);
            entity.setSongName(recentlyPlay.getSongName());
            entity.setSingerName(recentlyPlay.getSingerName());
            entity.setDuration(recentlyPlay.getDuration());
            entity.setPlayUrl(recentlyPlay.getPlayUrl());
            entity.setResource("网络");
            entity.setSongId(recentlyPlay.getSongId());
            recentlyPlayDao.insertOrReplace(entity);
            List<MusicInfo> musicInfoList = new ArrayList<>();
            for (RecentlyPlay play : recentlyPlayList) {
                MusicInfo musicInfo = new MusicInfo();
                musicInfo.setUrl(play.getPlayUrl());
                musicInfo.setSingerName(play.getSingerName());
                musicInfo.setSongName(play.getSongName());
                musicInfo.setDuration(play.getDuration());
                musicInfo.setResouce("本地");
                musicInfoList.add(musicInfo);
            }
            EventBus.getDefault().post(new MyEventBus.Deliver(musicInfoList, recentlyPlay.getSongName()));
            intent.putExtra("songName", recentlyPlay.getSongName());
            intent.putExtra("singerName", recentlyPlay.getSingerName());
            intent.putExtra("duration", recentlyPlay.getDuration());
            intent.putExtra("resource", "本地");
        }
        startActivity(intent);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.singer_back:
                finish();
                break;
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (resource.equals("本地音乐")) {

        } else if (resource.equals("我的最爱")) {
            delete_collection = (Collection) parent.getItemAtPosition(position);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("删除收藏");
            builder.setMessage("确定删除此首歌曲？");
            builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    collectionDao.queryBuilder().where(CollectionDao.Properties.SongId
                            .eq(delete_collection.getSongId())).buildDelete()
                            .executeDeleteWithoutDetachingEntities();
                    collectionAdapter.longClick(delete_collection);
                }
            });
            builder.setPositiveButton("取消", null);
            builder.show();
        } else if (resource.equals("最近播放")) {
            delete_recentlyPlay = (RecentlyPlay) parent.getItemAtPosition(position);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("删除记录");
            builder.setMessage("确定删除此首歌曲？");
            builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    recentlyPlayDao.queryBuilder().where(RecentlyPlayDao.Properties.SongId
                            .eq(delete_recentlyPlay.getSongId())).buildDelete()
                            .executeDeleteWithoutDetachingEntities();
                    recentlyPlayAdapter.longClick(delete_recentlyPlay);
                }
            });
            builder.setPositiveButton("取消", null);
            builder.show();
        }
        return false;
    }
}
